#include<iostream>
using namespace std;
int main()
{
    int gir,gir_1;
    cin>>gir;
    gir_1=gir;
    if(gir<10000 && gir>999){
        int f[10];
        for (int i=0; i<4; i++){
            f[i]=gir_1%10;
            gir_1=gir_1/10;
        }
        gir_1=gir;
        for (int i=4; i<7; i++){
            f[i]=gir_1%100;
            gir_1=gir_1/10;
        }
        gir_1=gir;
        for (int i=7; i<9; i++){
            f[i]=gir_1%1000;
            gir_1=gir_1/10;
        }
        gir_1=gir;
        f[9]=gir;
        int g[10]={0,0,0,0,0,0,0,0,0,0};
        for (int i=0; i<10;i++){
            int k=f[i],l=0;
            for (int j=1; j<k+1; j++){
                if(k%j==0){
                    l++;
                }
            }
            if(l==2){
                g[i]=k;
            }
        }
        int max=0;
        for (int i=0; i<10;i++){
            if(max<g[i]){
                max=g[i];
            }
        }
        cout<<max;
    }
    else{
        cout<<0;
    }
    return 0;
}
